package com.relife.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.model.AjaxJson;
import com.relife.common.model.ClientManager;
import com.relife.common.model.Page;
import com.relife.common.utils.StringUtil;
import com.relife.core.easyui.Easyui;
import com.relife.core.entity.SqlSetEntity;
import com.relife.core.qbc.QbcQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * sql语句配置
 * 
 * @author WU
 * @date 2016-11-28 11:46:52
 */
@Controller
@RequestMapping("sys/sqlSet")
public class SqlSetController extends BaseController {
	@RequestMapping
	public String sqlSetList(HttpServletRequest req) {
		return "sys/sqlSet/sqlSetList";
	}

	/**
	 * 数据列表
	 * 
	 * @author WU
	 * @date 2016-11-28 11:46:52
	 * @param sqlSet
	 * @param page
	 */
	@RequestMapping
	public void listData(SqlSetEntity sqlSet, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(sqlSet);
		service.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

	/**
	 * @author WU
	 * @date 2016-11-28 11:46:52
	 * @param sqlSet
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String sqlSet(SqlSetEntity sqlSet, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(sqlSet.getId())) {
			sqlSet = service.get(SqlSetEntity.class, sqlSet.getId());
		}
		req.setAttribute("page", sqlSet);
		return "sys/sqlSet/sqlSet";
	}

	/**
	 * 保存
	 * 
	 * @author WU
	 * @date 2016-11-28 11:46:52
	 * @param sqlSet
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson save(SqlSetEntity sqlSet, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			service.saveOrUpdate(sqlSet);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @author WU
	 * @date 2016-11-28 11:46:52
	 * @param sqlSet
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson del(SqlSetEntity sqlSet) {
		AjaxJson j = new AjaxJson();
		try {
			service.delete(sqlSet);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @author WU
	 * @date 2016-11-28 11:46:52
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson reloadSql() {
		AjaxJson j = new AjaxJson();
		try {
			ClientManager.getInstance().clearSql();
			List<SqlSetEntity> list = service.getList(new QbcQuery(SqlSetEntity.class));
			ClientManager.getInstance().addSql(list);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;
	}
}
